package com.server;

import com.server.util.HttpProtocolUtil;
import com.server.util.StaticResourceUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author lulei
 * @date 2020年12月25日 11:44:02
 */
public class Responese {
    private OutputStream outputStream;
    public Responese(){

    }
    public Responese(OutputStream outputStream){
        this.outputStream = outputStream;
    }

    /**
     * 字符串输出
     * @param content
     */
    public void outpuStr(String content) throws IOException {
        content =  HttpProtocolUtil.getHttpStatus200(content.getBytes().length)+content;
        this.outputStream.write(content.getBytes());
    }

    /**
     * 文件输出
     * @param path
     */
    public void outpuFile(String path) throws IOException {
        String absolutePath = StaticResourceUtil.getAbsolutePath(path);
        File file = new File(absolutePath);
        if(file.exists() && file.isFile()){//文件存在且是文件不是目录，输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),this.outputStream);
        }else{//文件找不到输入404
            this.outputStream.write(HttpProtocolUtil.getHttpHeader404().getBytes());
        }
        //Responese.class.getClassLoader().getResources()
    }
}
